<?php

namespace App\Models;
use App\Http\Requests\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;

class Register extends BaseModel
{
    protected $table = 'member';

    protected $primaryKey = 'id';

    public $timestamps = false;

    protected $guarded = [];

    public function getOne($where, $fileds = '*', $order = '')
    {
        if (!$order) {
            $order = ['id' => 'desc'];
        }
        return $this->multiSelect($fileds)->multiWhere($where)->multiOrder($order)->first();
    }

    /**
     * 按条件查询全部数据,根据配置显示条数显示
     */
    public function getList(array $where = [], $fields = '*', $order = '', $pageSize = '')
    {
        if ($pageSize) {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->paginate($pageSize);
        } else {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->get();
        }
    }

    /**
     *插入数据
     */
    public function add($data)
    {
        return $this->_add($data);
    }


    /***
     * @param array $id
     * @param array $data
     * 更新数据
     */
    public function up($where, $data)
    {
        return $this->_updata($where, $data);
    }

    /**
     * @param $id
     * 删除数据
     */
    public function del($where)
    {
        return $this->_del($where);
    }

    /**
     * 用户注册
     */
    public function register($request)
    {
        //手机用户注册信息
        $data['member_mobile'] = $request->input('member_mobile');
        $data['member_passwd'] = md5($request->input('member_passwd'));
        $data['trade_passwd']  = md5($request->input('trade_passwd'));
        $data['ip']            = getIp();
        $data['created_at'] = date('Y-m-d H:i:s',time());
        $data['updated_at'] = date('Y-m-d H:i:s',time());
        $code1          = $request->input('code');
        $where['member_mobile'] = $request->input('member_mobile');
        //正则匹配验证注册密码以及交易密码中是否含有特殊字符
        $member_passwd = $request->input('member_passwd');
        $trade_passwd = $request->input('trade_passwd');
        $pat = '/[^a-zA-z0-9]/';
        $member = preg_match($pat,$member_passwd);
        if ($member) {
            $res['state'] = 2;
            $res['msg'] = '登录密码含有非法字符!';
            return $res;
        }
        $trade = preg_match($pat,$trade_passwd);
        if ($trade) {
            $res['state'] = 3;
            $res['msg'] = '交易密码含有非法字符!';
            return $res;
        }
        if (!check_captcha($code1)) {
            $res['state'] = 0;
            $res['msg'] = '验证码错误';
            return $res;
        }
        $all = $this->getOne($where);
        if (empty($all['member_mobile'])) {
            $result = $this->add($data);
            if ( $result ) {
                // 生成邀请码
                $where1['code'] = getRandNum(4);
                $res = $this->getOne($where1);
                if ($res == '') {
                    $code = getRandNum(4);
                    $updata['code'] = $code;
                    $this->up(['id'=>$result],$updata);
                }
                // 判断是否为邀请链接过来
                if( $request['code2'] ) {
                     $re = $this->getOne(array("code"=>$request['code2']));
                     $update['member_id'] = $re['id'];
                     $update['mobile'] = $data['member_mobile'];
                     $update['mid'] = $result;
                     $update['created_at'] = date('Y-m-d H:i:s',time());
                     $update['updated_at'] = date('Y-m-d H:i:s',time());
                     $invite = new Invite();
                     $res1 = $invite->add($update);
                     //更新增加邀请数量
                     $up['invite_num'] = $re['invite_num']+1;
                     $this->up(['id'=>$re['id']],$up);
                }

                $res['state'] = 200;
                $res['msg'] = '注册成功！';
                return $res;
            }else{
                $res['state'] = 400;
                $res['msg'] = '注册失败！';
                return $res;
            }
        }
            $res['state'] = 1;
            $res['msg'] = '用户已注册！';
            return $res;


    }

}
